<template>
  <div class="_wz_cent_main _wz_positionAdd">
    <Form ref="formAdd" :model="formData" label-position="right" :label-width="100" :rules="ruleValidate">
      <FormItem label="运输企业" prop="deptUuid">
        <i-select v-model="formData.deptUuid" filterable placeholder="请选择运输企业" clearable @on-change="enterpriseGroup" :disabled="!!formData.id">
          <Option v-for="item in subDeptList" :value="item.uuid" :key="item.uuid">{{ item.deptName }}</Option>
        </i-select>
      </FormItem>
      <template v-if="!!formData.id">
        <FormItem label="上级部门" prop="parentUuid" v-if="formData.parentUuid != 0">
          <Select v-model="formData.parentUuid" placeholder="请选择上级部门">
            <Option v-for="item in treeList" :value="item.value" :key="item.value" style="display: none;">{{ item.title }}</Option>
            <Tree :data="treeData" @on-select-change="handleCheckChange" style="padding: 10px;"></Tree>
          </Select>
        </FormItem>
      </template>
      <template v-else-if="parentData">
        <FormItem label="上级部门" prop="parentUuid">
          <Select v-model="formData.parentUuid" placeholder="请选择上级部门">
            <Option v-for="item in treeList" :value="item.value" :key="item.value" style="display: none;">{{ item.title }}</Option>
            <Tree :data="treeData" @on-select-change="handleCheckChange" style="padding: 10px;"></Tree>
          </Select>
        </FormItem>
      </template>
      <FormItem label="部门类型" prop="inOrgType">
        <Select v-model="formData.inOrgType" placeholder="请选择部门类型">
          <Option :value="1">管理型单位</Option>
          <Option :value="2">生产型单位</Option>
        </Select>
      </FormItem>
      <FormItem label="部门名称" prop="inOrgName">
        <i-input v-model="formData.inOrgName" placeholder="请填写部门名称" clearable />
      </FormItem>
      <div class="demo-drawer-footer">
        <Button style="margin-right: 8px" @click="$emit('closeFun', false)">取消</Button>
        <Button type="primary" @click="addSubmit('formAdd')">提交</Button>
      </div>
    </Form>
  </div>
</template>

<script>

export default {
  props: ["selectData"],
  data() {
    return {
      parentData:false,//上级部门显隐
      subDeptList: [],//运输企业
      treeList: [],
      treeData: [],
      formTitle: "",
      formData:{},
      ruleValidate: {
        deptUuid: [
          {required: true, message: '请选择运输企业', trigger: 'blur'},
        ],
        inOrgName: [
          {required: true, message: '请填写部门名称', trigger: 'blur'},
        ],
        parentUuid: [
          {required: true, message: '请选择上级部门', trigger: 'blur'},
        ],
        inOrgType: [
          {type:'number',required: true, message: '请选择部门类型', trigger: 'blur'},
        ],
      },
    }
  },
  beforeCreate() {//beforeCreate创建前状态

  },
  created() {//created创建完毕状态

  },
  beforeMount() {//beforeMount挂载前状态   DOM 元素然后塞入页面的过程

  },
  mounted() {//mounted 挂载结束状态
    let that = this;

    that.formData = that.selectData;
    if(!!that.formData.parentUuid){
      that.treeList = [{value:that.formData.parentUuid,title:that.formData.parentName}]
    }

    //运输企业
    that.axios.post(that.apiUrl.coreDeptCoreDeptList, {}).then(res => {
      if (!!res) {
        that.subDeptList = res.data.data;
        if(!that.selectData.id){
          that.formData.deptUuid = that.subDeptList[0].uuid
        }
        that.enterpriseGroup()
      }
    }).catch(err => {
      console.log("失败", err)
    })

  },
  methods: {//执行的方法

    //企业级联
    enterpriseGroup(){
      let that = this;
      that.departmentList = [];
      if(!!that.formData.deptUuid){
        that.axios.post(that.apiUrl.coreInOrgCoreInOrgList,{deptUuid:that.formData.deptUuid}).then(res => {
          if (!!res) {
            var treeList = res.data.data;
            if(treeList.length > 0){
              that.parentData = true;
            }else{
              that.parentData = false;
            }

            treeList.forEach(function (k, i) {
              k.title = k.inOrgName;
              k.value = k.uuid;
            })
            //下拉表格
            var treeAttr = {
              id: 'uuid',
              parendId: 'parentUuid',
              name: 'inOrgName',
              rootId: 0
            }
            that.treeData = that.public.toTreeData(JSON.parse(JSON.stringify(treeList)), treeAttr);
          }
        }).catch(err => {
          console.log("失败", err)
        })
      }
    },
    //上级部门
    handleCheckChange(data){
      let that = this;
      if(data.length == 0){
        return false;
      }
      if(data[0].id == that.formData.uuid){
        that.$Message.error('上级部门不能选择自己!!');
        return false;
      }
      that.treeList = [{value:data[0].uuid,title:data[0].inOrgName}]
      that.formData.parentUuid = data[0].uuid;
      that.formData.parentName = data[0].name;
    },
    //提交
    addSubmit(name){
      var that = this
      that.$refs[name].validate((valid) => {
        if (valid) {//正则判断使用
          that.$Spin.show()
          that.axios.post(that.apiUrl.coreInOrgCoreInOrgSaveOrUpdate, that.formData).then(res => {
            that.$Spin.hide()
            if (!!res) {
              that.$Message.success('保存成功');
              that.$emit("closeFun", true)
            }
          }).catch(err => {
            console.log("失败", err)
          })
        } else {
          that.$Message.error('请正确填写表单');
        }
      });
    },
  },
  watch: {//监听
  },
  beforeUpdate() {//beforeUpdate 更新前状态

  },
  updated() {//updated 更新完成状态

  },
  beforeDestroy() {//beforeDestroy 销毁前状态

  },
  destroyed() {//destroyed 销毁完成状态

  }
}
</script>
<style lang="less">
._wz_positionAdd {
}
</style>
